# import tkinter as tk

# root = tk.Tk()
# root.title("多个 Frame 示例")

# # === 第一个 Frame（区域1）===
# frame1 = tk.Frame(root, bd=2, relief="groove")
# frame1.grid(row=0, column=0, padx=10, pady=10)

# tk.Label(frame1, text="区域 1").grid(row=0, column=0, columnspan=2)
# tk.Label(frame1, text="姓名:").grid(row=1, column=0)
# tk.Entry(frame1).grid(row=1, column=1)

# tk.Label(frame1, text="年龄:").grid(row=2, column=0)
# tk.Entry(frame1).grid(row=2, column=1)

# # === 第二个 Frame（区域2）===
# frame2 = tk.Frame(root, bd=2, relief="groove")
# frame2.grid(row=0, column=1, padx=10, pady=10)

# tk.Label(frame2, text="区域 2").grid(row=0, column=0, columnspan=2)
# tk.Button(frame2, text="提交").grid(row=1, column=0)
# tk.Button(frame2, text="取消").grid(row=1, column=1)

# # === 第三个 Frame（底部区域）===
# frame3 = tk.Frame(root)
# frame3.grid(row=1, column=0, columnspan=2, pady=10)

# tk.Label(frame3, text="这是一个底部状态栏").pack()

# root.mainloop()

# import customtkinter as ctk

# # 设置主题
# ctk.set_appearance_mode("light")
# ctk.set_default_color_theme("blue")

# # 创建主窗口
# root = ctk.CTk()
# root.title("Custom Button Example")

# # 创建按钮
# button = ctk.CTkButton(root, text="+ 新建对话", corner_radius=30,
#                        fg_color="#6A5ACD", text_color="white", font=("Arial", 12))
# button.pack(pady=20)

# # 运行主循环
# root.mainloop()





# import customtkinter as ctk
# from tkinter import messagebox
# from PIL import Image, ImageTk
# import random

# ctk.set_appearance_mode("dark")
# ctk.set_default_color_theme("blue")

# class DeviceMonitor(ctk.CTk):
#     def __init__(self):
#         super().__init__()
#         self.title("组态界面 - 设备监控")
#         self.geometry("600x400")

#         self.temperature = 25.0
#         self.is_running = False

#         # 加载图片
#         self.pump_img = Image.open("./pt/1753581540312.png").resize((50, 50))
#         self.pump_icon = ImageTk.PhotoImage(self.pump_img)

#         # 图标
#         self.pump_label = ctk.CTkLabel(self, image=self.pump_icon, text="")
#         self.pump_label.place(x=50, y=50)

#         # 状态灯
#         self.status_light = ctk.CTkLabel(self, text="", width=20, height=20, fg_color="red", corner_radius=10)
#         self.status_light.place(x=110, y=60)

#         # 控制按钮
#         self.toggle_button = ctk.CTkButton(self, text="启动设备", command=self.toggle_device)
#         self.toggle_button.place(x=50, y=120)

#         # 温度显示
#         self.temp_label = ctk.CTkLabel(self, text=f"温度: {self.temperature:.1f}°C", font=("Arial", 16))
#         self.temp_label.place(x=50, y=170)

#         # 启动定时更新
#         self.update_status()

#     def toggle_device(self):
#         self.is_running = not self.is_running
#         self.toggle_button.configure(text="停止设备" if self.is_running else "启动设备")
#         self.status_light.configure(fg_color="green" if self.is_running else "red")

#     def update_status(self):
#         if self.is_running:
#             self.temperature += random.uniform(-0.5, 1.0)
#             self.temp_label.configure(text=f"温度: {self.temperature:.1f}°C")
#         self.after(1000, self.update_status)

# if __name__ == "__main__":
#     app = DeviceMonitor()
#     app.mainloop()

# import customtkinter as ctk
# from PIL import Image, ImageTk
# import tkinter as tk

# ctk.set_appearance_mode("dark")
# ctk.set_default_color_theme("blue")

# class DraggableComponent(ctk.CTkLabel):
#     def __init__(self, master, image_path=None, text="组件", **kwargs):
#         super().__init__(master, text=text, **kwargs)
#         self.master = master
#         self.offset_x = 0
#         self.offset_y = 0

#         if image_path:
#             self.load_image(image_path)

#         self.bind("<Button-1>", self.on_click)
#         self.bind("<B1-Motion>", self.on_drag)

#     def load_image(self, path):
#         img = Image.open(path).resize((50, 50))
#         self.image = ImageTk.PhotoImage(img)
#         self.configure(image=self.image, text="")

#     def on_click(self, event):
#         # 获取点击位置与组件左上角的偏移量
#         self.offset_x = event.x
#         self.offset_y = event.y

#     def on_drag(self, event):
#         x = self.winfo_x() - self.offset_x + event.x
#         y = self.winfo_y() - self.offset_y + event.y
#         self.place(x=x, y=y)

#         # 检查是否拖拽到目标区域
#         self.check_drop_target(event)

#     def check_drop_target(self, event):
#         x_root = self.winfo_rootx() + event.x
#         y_root = self.winfo_rooty() + event.y

#         # 获取目标区域坐标
#         target_x1 = self.master.target_frame.winfo_rootx()
#         target_y1 = self.master.target_frame.winfo_rooty()
#         target_x2 = target_x1 + self.master.target_frame.winfo_width()
#         target_y2 = target_y1 + self.master.target_frame.winfo_height()

#         # 检查是否在目标区域内
#         if (target_x1 < x_root < target_x2) and (target_y1 < y_root < target_y2):
#             self.on_drop_in_target()

#     def on_drop_in_target(self):
#         print("组件已拖入目标区域！")
#         self.place(x=10, y=10, in_=self.master.target_frame)  # 放置在目标区域内部
#         self.master.on_component_dropped(self)


# class DropTargetFrame(ctk.CTkFrame):
#     def __init__(self, master, **kwargs):
#         super().__init__(master, width=200, height=200, fg_color="gray20", **kwargs)
#         self.label = ctk.CTkLabel(self, text="拖拽组件到此处")
#         self.label.pack(expand=True)


# class ConfiguratorApp(ctk.CTk):
#     def __init__(self):
#         super().__init__()
#         self.title("组态界面 - 拖拽示例")
#         self.geometry("800x600")

#         # 创建目标区域
#         self.target_frame = DropTargetFrame(self)
#         self.target_frame.place(x=500, y=100)

#         # 创建可拖拽组件
#         self.drag_component1 = DraggableComponent(self, image_path="./pt/1753581540312.png", text="水泵")
#         self.drag_component1.place(x=50, y=50)

#         self.drag_component2 = DraggableComponent(self, image_path="./pt/1753581540312.png", text="阀门")
#         self.drag_component2.place(x=50, y=120)

#     def on_component_dropped(self, component):
#         print(f"{component.cget('text')} 已组合到目标区域")


# if __name__ == "__main__":
#     app = ConfiguratorApp()
#     app.mainloop()



import tkinter
import tkinter.messagebox
import customtkinter

customtkinter.set_appearance_mode("System")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"


class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        # configure window
        self.title("CustomTkinter complex_example.py")
        self.geometry(f"{1100}x{580}")

        # configure grid layout (4x4)
        self.grid_columnconfigure(1, weight=1)
        self.grid_columnconfigure((2, 3), weight=0)
        self.grid_rowconfigure((0, 1, 2), weight=1)

        # create sidebar frame with widgets
        self.sidebar_frame = customtkinter.CTkFrame(self, width=140, corner_radius=0)
        self.sidebar_frame.grid(row=0, column=0, rowspan=4, sticky="nsew")
        self.sidebar_frame.grid_rowconfigure(4, weight=1)
        self.logo_label = customtkinter.CTkLabel(self.sidebar_frame, text="CustomTkinter", font=customtkinter.CTkFont(size=20, weight="bold"))
        self.logo_label.grid(row=0, column=0, padx=20, pady=(20, 10))
        self.sidebar_button_1 = customtkinter.CTkButton(self.sidebar_frame, command=self.sidebar_button_event)
        self.sidebar_button_1.grid(row=1, column=0, padx=20, pady=10)
        self.sidebar_button_2 = customtkinter.CTkButton(self.sidebar_frame, command=self.sidebar_button_event)
        self.sidebar_button_2.grid(row=2, column=0, padx=20, pady=10)
        self.sidebar_button_3 = customtkinter.CTkButton(self.sidebar_frame, command=self.sidebar_button_event)
        self.sidebar_button_3.grid(row=3, column=0, padx=20, pady=10)
        self.appearance_mode_label = customtkinter.CTkLabel(self.sidebar_frame, text="Appearance Mode:", anchor="w")
        self.appearance_mode_label.grid(row=5, column=0, padx=20, pady=(10, 0))
        self.appearance_mode_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame, values=["Light", "Dark", "System"],
                                                                       command=self.change_appearance_mode_event)
        self.appearance_mode_optionemenu.grid(row=6, column=0, padx=20, pady=(10, 10))
        self.scaling_label = customtkinter.CTkLabel(self.sidebar_frame, text="UI Scaling:", anchor="w")
        self.scaling_label.grid(row=7, column=0, padx=20, pady=(10, 0))
        self.scaling_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame, values=["80%", "90%", "100%", "110%", "120%"],
                                                               command=self.change_scaling_event)
        self.scaling_optionemenu.grid(row=8, column=0, padx=20, pady=(10, 20))

        # create main entry and button
        self.entry = customtkinter.CTkEntry(self, placeholder_text="CTkEntry")
        self.entry.grid(row=3, column=1, columnspan=2, padx=(20, 0), pady=(20, 20), sticky="nsew")

        self.main_button_1 = customtkinter.CTkButton(master=self, fg_color="transparent", border_width=2, text_color=("gray10", "#DCE4EE"))
        self.main_button_1.grid(row=3, column=3, padx=(20, 20), pady=(20, 20), sticky="nsew")

        # create textbox
        self.textbox = customtkinter.CTkTextbox(self, width=250)
        self.textbox.grid(row=0, column=1, padx=(20, 0), pady=(20, 0), sticky="nsew")

        # create tabview
        self.tabview = customtkinter.CTkTabview(self, width=250)
        self.tabview.grid(row=0, column=2, padx=(20, 0), pady=(20, 0), sticky="nsew")
        self.tabview.add("CTkTabview")
        self.tabview.add("Tab 2")
        self.tabview.add("Tab 3")
        self.tabview.tab("CTkTabview").grid_columnconfigure(0, weight=1)  # configure grid of individual tabs
        self.tabview.tab("Tab 2").grid_columnconfigure(0, weight=1)

        self.optionmenu_1 = customtkinter.CTkOptionMenu(self.tabview.tab("CTkTabview"), dynamic_resizing=False,
                                                        values=["Value 1", "Value 2", "Value Long Long Long"])
        self.optionmenu_1.grid(row=0, column=0, padx=20, pady=(20, 10))
        self.combobox_1 = customtkinter.CTkComboBox(self.tabview.tab("CTkTabview"),
                                                    values=["Value 1", "Value 2", "Value Long....."])
        self.combobox_1.grid(row=1, column=0, padx=20, pady=(10, 10))
        self.string_input_button = customtkinter.CTkButton(self.tabview.tab("CTkTabview"), text="Open CTkInputDialog",
                                                           command=self.open_input_dialog_event)
        self.string_input_button.grid(row=2, column=0, padx=20, pady=(10, 10))
        self.label_tab_2 = customtkinter.CTkLabel(self.tabview.tab("Tab 2"), text="CTkLabel on Tab 2")
        self.label_tab_2.grid(row=0, column=0, padx=20, pady=20)

        # create radiobutton frame
        self.radiobutton_frame = customtkinter.CTkFrame(self)
        self.radiobutton_frame.grid(row=0, column=3, padx=(20, 20), pady=(20, 0), sticky="nsew")
        self.radio_var = tkinter.IntVar(value=0)
        self.label_radio_group = customtkinter.CTkLabel(master=self.radiobutton_frame, text="CTkRadioButton Group:")
        self.label_radio_group.grid(row=0, column=2, columnspan=1, padx=10, pady=10, sticky="")
        self.radio_button_1 = customtkinter.CTkRadioButton(master=self.radiobutton_frame, variable=self.radio_var, value=0)
        self.radio_button_1.grid(row=1, column=2, pady=10, padx=20, sticky="n")
        self.radio_button_2 = customtkinter.CTkRadioButton(master=self.radiobutton_frame, variable=self.radio_var, value=1)
        self.radio_button_2.grid(row=2, column=2, pady=10, padx=20, sticky="n")
        self.radio_button_3 = customtkinter.CTkRadioButton(master=self.radiobutton_frame, variable=self.radio_var, value=2)
        self.radio_button_3.grid(row=3, column=2, pady=10, padx=20, sticky="n")

        # create slider and progressbar frame
        self.slider_progressbar_frame = customtkinter.CTkFrame(self, fg_color="transparent")
        self.slider_progressbar_frame.grid(row=1, column=1, padx=(20, 0), pady=(20, 0), sticky="nsew")
        self.slider_progressbar_frame.grid_columnconfigure(0, weight=1)
        self.slider_progressbar_frame.grid_rowconfigure(4, weight=1)
        self.seg_button_1 = customtkinter.CTkSegmentedButton(self.slider_progressbar_frame)
        self.seg_button_1.grid(row=0, column=0, padx=(20, 10), pady=(10, 10), sticky="ew")
        self.progressbar_1 = customtkinter.CTkProgressBar(self.slider_progressbar_frame)
        self.progressbar_1.grid(row=1, column=0, padx=(20, 10), pady=(10, 10), sticky="ew")
        self.progressbar_2 = customtkinter.CTkProgressBar(self.slider_progressbar_frame)
        self.progressbar_2.grid(row=2, column=0, padx=(20, 10), pady=(10, 10), sticky="ew")
        self.slider_1 = customtkinter.CTkSlider(self.slider_progressbar_frame, from_=0, to=1, number_of_steps=4)
        self.slider_1.grid(row=3, column=0, padx=(20, 10), pady=(10, 10), sticky="ew")
        self.slider_2 = customtkinter.CTkSlider(self.slider_progressbar_frame, orientation="vertical")
        self.slider_2.grid(row=0, column=1, rowspan=5, padx=(10, 10), pady=(10, 10), sticky="ns")
        self.progressbar_3 = customtkinter.CTkProgressBar(self.slider_progressbar_frame, orientation="vertical")
        self.progressbar_3.grid(row=0, column=2, rowspan=5, padx=(10, 20), pady=(10, 10), sticky="ns")

        # create scrollable frame
        self.scrollable_frame = customtkinter.CTkScrollableFrame(self, label_text="CTkScrollableFrame")
        self.scrollable_frame.grid(row=1, column=2, padx=(20, 0), pady=(20, 0), sticky="nsew")
        self.scrollable_frame.grid_columnconfigure(0, weight=1)
        self.scrollable_frame_switches = []
        for i in range(100):
            switch = customtkinter.CTkSwitch(master=self.scrollable_frame, text=f"CTkSwitch {i}")
            switch.grid(row=i, column=0, padx=10, pady=(0, 20))
            self.scrollable_frame_switches.append(switch)

        # create checkbox and switch frame
        self.checkbox_slider_frame = customtkinter.CTkFrame(self)
        self.checkbox_slider_frame.grid(row=1, column=3, padx=(20, 20), pady=(20, 0), sticky="nsew")
        self.checkbox_1 = customtkinter.CTkCheckBox(master=self.checkbox_slider_frame)
        self.checkbox_1.grid(row=1, column=0, pady=(20, 0), padx=20, sticky="n")
        self.checkbox_2 = customtkinter.CTkCheckBox(master=self.checkbox_slider_frame)
        self.checkbox_2.grid(row=2, column=0, pady=(20, 0), padx=20, sticky="n")
        self.checkbox_3 = customtkinter.CTkCheckBox(master=self.checkbox_slider_frame)
        self.checkbox_3.grid(row=3, column=0, pady=20, padx=20, sticky="n")

        # set default values
        self.sidebar_button_3.configure(state="disabled", text="Disabled CTkButton")
        self.checkbox_3.configure(state="disabled")
        self.checkbox_1.select()
        self.scrollable_frame_switches[0].select()
        self.scrollable_frame_switches[4].select()
        self.radio_button_3.configure(state="disabled")
        self.appearance_mode_optionemenu.set("Dark")
        self.scaling_optionemenu.set("100%")
        self.optionmenu_1.set("CTkOptionmenu")
        self.combobox_1.set("CTkComboBox")
        self.slider_1.configure(command=self.progressbar_2.set)
        self.slider_2.configure(command=self.progressbar_3.set)
        self.progressbar_1.configure(mode="indeterminnate")
        self.progressbar_1.start()
        self.textbox.insert("0.0", "CTkTextbox\n\n" + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.\n\n" * 20)
        self.seg_button_1.configure(values=["CTkSegmentedButton", "Value 2", "Value 3"])
        self.seg_button_1.set("Value 2")

    def open_input_dialog_event(self):
        dialog = customtkinter.CTkInputDialog(text="Type in a number:", title="CTkInputDialog")
        print("CTkInputDialog:", dialog.get_input())

    def change_appearance_mode_event(self, new_appearance_mode: str):
        customtkinter.set_appearance_mode(new_appearance_mode)

    def change_scaling_event(self, new_scaling: str):
        new_scaling_float = int(new_scaling.replace("%", "")) / 100
        customtkinter.set_widget_scaling(new_scaling_float)

    def sidebar_button_event(self):
        print("sidebar_button click")


if __name__ == "__main__":
    app = App()
    app.mainloop()
